from pymysql import Connection
# 获取到MySQL数据库的链接对象
conn = Connection(
    host = "localhost",#主机名
    port = 3306,#端口
    user = "root",
    password="123456"
)
# print(conn.get_server_info())

cursor = conn.cursor()  #使用 cursor() 方法创建一个游标对象 cursor
# 选择数据库
conn.select_db("test")
# 执行sql
cursor.execute("create table test_pymysql(id int, name varchar(10), age int, gender varchar(1));")
# conn.close()
conn.select_db("world")
# 执行sql
cursor.execute("select * from country")
results = cursor.fetchall() # fetchall将查询结果封装到元组内
for r in results:
    print(r)
# 关闭数据库链接
conn.close()
